package com.ct.lotterysystem2.controller;

import com.ct.lotterysystem2.common.errorcode.ControllerErrorCodeConstants;
import com.ct.lotterysystem2.common.exception.ControllerException;
import com.ct.lotterysystem2.common.pojo.CommonResult;
import com.ct.lotterysystem2.common.utils.JacksonUtil;
import com.ct.lotterysystem2.controller.param.ShortMessageLoginParam;
import com.ct.lotterysystem2.controller.param.UserPasswordLoginParam;
import com.ct.lotterysystem2.controller.param.UserRegisterParam;
import com.ct.lotterysystem2.controller.result.BaseUserInfoParam;
import com.ct.lotterysystem2.controller.result.UserLoginResult;
import com.ct.lotterysystem2.controller.result.UserRegisterResult;
import com.ct.lotterysystem2.service.UserService;
import com.ct.lotterysystem2.service.VerificationCodeService;
import com.ct.lotterysystem2.service.dto.UserDTO;
import com.ct.lotterysystem2.service.dto.UserLoginDTO;
import com.ct.lotterysystem2.service.dto.UserRegisterDTO;
import com.ct.lotterysystem2.service.enums.UserIdentityEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private VerificationCodeService verificationCodeService;

    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> userRegister (
            @Validated @RequestBody UserRegisterParam param) {
        log.info("userRegister UserRegisterParam:{}",
                JacksonUtil.writerValueAsString(param));
        UserRegisterDTO userRegisterDTO = userService.register(param);
        return CommonResult.success(convertToUserRegisterResult(userRegisterDTO));
    }

    @RequestMapping("/verification-code/send")
    public CommonResult<Boolean> sendVerificationCode (
            @RequestParam("phoneNumber") String email) {
        log.info("sendVerificationCode phoneNumber={}", email);
        verificationCodeService.sendVerificationCode(email);
        return CommonResult.success(Boolean.TRUE);
    }


    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> userPasswordLogin(
            @Validated @RequestBody UserPasswordLoginParam param) {
        log.info("userPasswordLogin UserPasswordLoginParam:{}",
                JacksonUtil.writerValueAsString(param));
        UserLoginDTO userLoginDTO = userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));
    }

    @RequestMapping("/message/login")
    public CommonResult<UserLoginResult> shortMessageLogin(
            @Validated @RequestBody ShortMessageLoginParam param) {
        log.info("shortMessageLogin ShortMessageLoginParam:{}",
                JacksonUtil.writerValueAsString(param));
        UserLoginDTO userLoginDTO = userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));
    }

    public CommonResult<List<BaseUserInfoParam>> findUserBaseInfoList (String identity) {
        log.info("findUserBaseInfoList identity={}", identity);
        List<UserDTO> userDTOList = userService.findUserInfo(UserIdentityEnum.forName(identity));
        return CommonResult.success(convertToBaseUserList(userDTOList));
    }

    private List<BaseUserInfoParam> convertToBaseUserList(List<UserDTO> userDTOList) {
        if (CollectionUtils.isEmpty(userDTOList)) {
            return Arrays.asList();
        }
        return userDTOList.stream()
                 .map(userDTO -> {
                     BaseUserInfoParam baseUserInfoParam = new BaseUserInfoParam();
                     baseUserInfoParam.setUserId(userDTO.getUserId());
                     baseUserInfoParam.setUserName(userDTO.getUserName());
                     baseUserInfoParam.setIdentity(userDTO.getIdentity().name());
                     return baseUserInfoParam;
                 }).collect(Collectors.toList());
    }

    private UserLoginResult convertToUserLoginResult(UserLoginDTO userLoginDTO) {
        if (null == userLoginDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        }
        UserLoginResult result = new UserLoginResult();
        result.setToken(userLoginDTO.getToken());
        result.setIdentify(userLoginDTO.getIdentity().name());
        return result;
    }

    private UserRegisterResult convertToUserRegisterResult(UserRegisterDTO userRegisterDTO) {
        if (null == userRegisterDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }
        UserRegisterResult result = new UserRegisterResult();
        result.setUserId(userRegisterDTO.getUserId());
        return result;
    }
}
